package com.ctbu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctbu.api.UserInfoService;
import com.ctbu.entity.FinancialContracts;
import com.ctbu.entity.FinancialProduct;
import com.ctbu.entity.Users;
import com.ctbu.mapper.FinancialContractsMapper;
import com.ctbu.mapper.FinancialProductMapper;
import com.ctbu.service.FinancialContractsService;
import com.ctbu.tools.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 金融衍生品合约信息表，包含合约ID、产品ID、用户ID、起始日期、结束日期、备注、合约单位、当前价格和约定价格等信息 服务实现类
 * </p>
 *
 * @author tanyan
 * @since 2024年06月27日
 */
@Service
public class FinancialContractsServiceImpl extends ServiceImpl<FinancialContractsMapper, FinancialContracts> implements FinancialContractsService {

    @Autowired
    private FinancialContractsMapper financialContractsMapper;

    @Autowired
    private FinancialProductMapper financialProductMapper;

    @Autowired
    private  UserInfoService userFeignService;

    @Override
    public ResponseResult<Object> selectList(int pageNum, int pageSize, Integer productId,Integer userId) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //条件查询
        LambdaQueryWrapper<FinancialContracts> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (productId!=null){
            //精准查询
            lambdaQueryWrapper.eq(FinancialContracts::getProductId,productId);
        }
        if (userId!=null){
            //精准查询
            lambdaQueryWrapper.eq(FinancialContracts::getUserId,userId);
        }
//        if (StringUtils.isNotBlank(contractUnits)){
//            lambdaQueryWrapper.like(FinancialContracts::getContractUnits,contractUnits);
//
//        }
        List<FinancialContracts> list = financialContractsMapper.selectList(lambdaQueryWrapper);

        //处理产品的数据
        list.forEach(financialContracts -> {
            FinancialProduct financialProduct = financialProductMapper.selectById(financialContracts.getProductId());
            //将产品信息封装到合约对象中
            financialContracts.setFinancialProduct(financialProduct);

            Users user = userFeignService.byId(financialContracts.getUserId());
            financialContracts.setUsers(user);
        });

        //将查询的结果封装
        PageInfo<FinancialContracts> pageInfo = new PageInfo<>(list);

        return ResponseResult.success(pageInfo,"查询成功");

    }
}
